use std::f32::consts::PI;

use crate::{generate_corner_face, generate_main_face};
use bevy::color::palettes::css::*;
use bevy::math::Affine2;
use bevy::render::primitives::*;
use bevy::{prelude::*, render::view::RenderLayers};
// use bevy_mod_outline::{
//     AsyncSceneInheritOutline, AsyncSceneInheritOutlinePlugin, OutlinePlugin, OutlineStencil,
//     OutlineVolume, ATTRIBUTE_OUTLINE_NORMAL,
// };

use super::{CubePart, ViewcubeHit};

pub fn setup(
    mut commands: Commands,
    mut meshes: ResMut<Assets<Mesh>>,
    mut materials: ResMut<Assets<StandardMaterial>>,
    mut asset_server: ResMut<AssetServer>,
) {
    let center = Vec3::new(0.6, 0.6, 0.6);
    commands
        .spawn((
            Mesh3d(meshes.add(bevy::prelude::Sphere::new(0.01).mesh().uv(1, 1))),
            MeshMaterial3d(materials.add(StandardMaterial::default())),
            RenderLayers::layer(13),
            super::ViewcubeCenter,
        ))
        .with_children(|builder| {
            builder.spawn((
                Mesh3d(meshes.add(crate::prelude::BevyTridentAxis::default())),
                MeshMaterial3d(materials.add(StandardMaterial::default())),
                Transform::from_translation(-center),
                RenderLayers::layer(13),
            ));
            generate_viewcube_simple_face(
                0.8f32,
                builder,
                &mut meshes,
                &mut materials,
                &mut asset_server,
            );
        });
}

pub fn generate_viewcube_simple_face(
    size: f32,
    builder: &mut ChildSpawnerCommands,
    meshes: &mut Assets<Mesh>,
    materials: &mut Assets<StandardMaterial>,
    mut asset_server: &mut AssetServer,
) {
    let front_mat = materials.add(StandardMaterial {
        base_color_texture: Some(asset_server.load("cube/front.png")),
        alpha_mode: AlphaMode::Blend,
        ..Default::default()
    });

    //todo 对当前的贴图能否也做相应的旋转变换
    let back_mat = materials.add(StandardMaterial {
        base_color_texture: Some(asset_server.load("cube/back.png")),
        alpha_mode: AlphaMode::Blend,
        uv_transform: Affine2 {
            matrix2: Mat2::from_angle(PI),
            translation: Vec2::new(1.0, 1.0),
        },
        ..Default::default()
    });

    let left_mat = materials.add(StandardMaterial {
        base_color_texture: Some(asset_server.load("cube/left.png")),
        alpha_mode: AlphaMode::Blend,
        uv_transform: Affine2 {
            matrix2: Mat2::from_angle(-PI / 2.0),
            translation: Vec2::new(0.0, 1.0),
        },
        ..Default::default()
    });

    let right_mat = materials.add(StandardMaterial {
        base_color_texture: Some(asset_server.load("cube/right.png")),
        alpha_mode: AlphaMode::Blend,
        uv_transform: Affine2 {
            matrix2: Mat2::from_angle(PI / 2.0),
            translation: Vec2::new(1.0, 0.0),
        },
        ..Default::default()
    });

    let top_mat = materials.add(StandardMaterial {
        base_color_texture: Some(asset_server.load("cube/top.png")),
        alpha_mode: AlphaMode::Blend,
        // uv_transform: Affine2 {
        //     matrix2: Mat2::from_angle(PI),
        //     translation: Vec2::new(0.0, 1.0),
        // },
        ..Default::default()
    });

    let bottom_mat = materials.add(StandardMaterial {
        base_color_texture: Some(asset_server.load("cube/bottom.png")),
        alpha_mode: AlphaMode::Blend,
        // uv_transform: Affine2 {
        //     matrix2: Mat2::from_angle(PI),
        //     translation: Vec2::new(0.0, 0.0),
        // },
        ..Default::default()
    });
    let plane = Plane3d::default().mesh().size(size, size);
    let half = 0.4f32;
    // Right (+X)
    generate_main_face!(
        builder,
        meshes,
        right_mat,
        plane,
        Transform::from_matrix(Mat4::from_rotation_translation(
            Quat::from_rotation_z(-crate::PI / 2.0),
            Vec3::new(half, 0.0, 0.0),
        )),
        ViewcubeHit(CubePart::Right)
    );
    // Left (-X)
    generate_main_face!(
        builder,
        meshes,
        left_mat,
        plane,
        Transform::from_matrix(Mat4::from_rotation_translation(
            Quat::from_rotation_z(crate::PI / 2.0),
            Vec3::new(-half, 0.0, 0.0),
        )),
        ViewcubeHit(CubePart::Left)
    );
    // Top (+Y)
    generate_main_face!(
        builder,
        meshes,
        top_mat,
        plane,
        Transform::from_matrix(Mat4::from_rotation_translation(
            Quat::from_rotation_x(0.0),
            Vec3::new(0.0, half, 0.0),
        )),
        ViewcubeHit(CubePart::Top)
    );
    // Bottom (-Y)
    generate_main_face!(
        builder,
        meshes,
        bottom_mat,
        plane,
        Transform::from_matrix(Mat4::from_rotation_translation(
            Quat::from_rotation_x(crate::PI),
            Vec3::new(0.0, -half, 0.0),
        )),
        ViewcubeHit(CubePart::Bottom)
    );
    // Front (+Z)
    generate_main_face!(
        builder,
        meshes,
        front_mat,
        plane,
        Transform::from_matrix(Mat4::from_rotation_translation(
            Quat::from_rotation_x(crate::PI / 2.0),
            Vec3::new(0.0, 0.0, half),
        )),
        ViewcubeHit(CubePart::Front)
    );
    // Back (-Z)
    generate_main_face!(
        builder,
        meshes,
        back_mat,
        plane,
        Transform::from_matrix(Mat4::from_rotation_translation(
            Quat::from_rotation_x(-crate::PI / 2.0),
            Vec3::new(0.0, 0.0, -half),
        )),
        ViewcubeHit(CubePart::Back)
    );
}
